package org.example.mone.task;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import org.example.constant.CommonConstant;
import org.example.enums.EJobType;
import org.example.enums.EUninstallStep;
import org.example.model.CheckHealthParam;
import org.example.model.CleanParam;
import org.example.model.UninstallParam;
import org.example.task.HttpTask;
import org.example.task.IExecutable;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

public class UninstallJob implements IExecutable {

    private static final Logger LOGGER = LoggerFactory.getLogger(UninstallJob.class);

    private RestTemplate restTemplate;

    public UninstallJob(EUninstallStep taskStep) {
        this.taskStep = taskStep;
        initBean();
    }

    public UninstallJob() {
        initBean();
    }

    private EUninstallStep taskStep;

    private void initBean() {
        restTemplate = SpringUtil.getBean(RestTemplate.class);
    }

    @Override
    public Result<String> execute() {
        LOGGER.info("UninstallJob start taskStep={}", taskStep);
        HttpTask httpTask = null;
        Result<String> result = null;
        switch (taskStep) {
            case CHECKHEALTH:
                LOGGER.info("UninstallJob CHECKHEALTH");
                CheckHealthParam checkHealthParam = new CheckHealthParam();
                checkHealthParam.setJobType(EJobType.UNINSTALL.getJobType());
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/checkhealth", JSONObject.toJSONString(checkHealthParam));
                result = httpTask.execute();
                break;
            case UNINSTALL:
                LOGGER.info("UninstallJob UNINSTALL");
                UninstallParam uninstallParam = new UninstallParam();
                uninstallParam.setFilePath("xxxxxx");
                uninstallParam.setTargetPath("xxxxxx2");
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/unInstallScript",JSONObject.toJSONString(uninstallParam));
                result = httpTask.execute();
                break;
            case CLEAN:
                LOGGER.info("UninstallJob CLEAN");
                CleanParam cleanParam = new CleanParam();
                cleanParam.setFilePath("xxxxxx");
                httpTask = new HttpTask(CommonConstant.SLAVE_HOST_URL + "agent/cleanDir",JSONObject.toJSONString(cleanParam));
                result = httpTask.execute();
                break;
            case END:
                LOGGER.info("UninstallJob END");
                break;
            default:
                LOGGER.error("UninstallJob error EUninstallStep ...taskStep={}",taskStep);
        }
        LOGGER.info("更新JOB任务状态。。。。。。。toDB,{}", result);
        return result;
    }
}
